package com.jiankang.controller;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jiankang.entity.AdviceForUser;
import com.jiankang.entity.User;
import com.jiankang.service.AdviceForUserService;
import com.jiankang.service.UserService;
import com.jiankang.util.Page;
@Controller
@RequestMapping("/advice")
public class AdviceForUserController {
	@Resource
	private AdviceForUserService adviceForUserService;
	// hehe当前时间
	Date now = new Date();
	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	String hehe = dateFormat.format(now);
	@Resource
	private UserService  userService ;
	@RequestMapping("/advicelist")
	public String advicelist(Model model, HttpServletRequest req)throws Exception {
		List<User> user=userService.getAllUser();
		String pageNow = req.getParameter("pageNow");
		Page page = null;
		List<AdviceForUser> adviceForUser=null;
		int totalCount = (int) adviceForUserService.getAllProductCount();
		if (pageNow != null) {
			page = new Page(totalCount, Integer.parseInt(pageNow));
			adviceForUser=adviceForUserService.getHealthFile(page);
		}else{
			page = new Page(totalCount, 1);
			adviceForUser=adviceForUserService.getHealthFile(page);
		}
		model.addAttribute("page", page);
		model.addAttribute("adviceForUser", adviceForUser);
		model.addAttribute("user", user);
		return "admin/advicelist";
	}
	@RequestMapping("/quest/{problemId}")
	public String quest(@PathVariable("problemId") int problemId, Model model){
		List<AdviceForUser> adviceForUser1=adviceForUserService.answer(problemId);
		List<AdviceForUser> adviceForUser=adviceForUserService.quest(problemId);
		model.addAttribute("adviceForUser", adviceForUser);
		model.addAttribute("adviceFor", adviceForUser1);
		return "admin/quest";
	}
	//回复
	@RequestMapping("/saveAddQuest")
	public String saveAddQuest(Model model,AdviceForUser adviceForUser,HttpServletRequest req) throws Exception{
		adviceForUser.setAnswerDate(hehe);
		adviceForUserService.saveAddQuest(adviceForUser);
		return this.advicelist(model,req);
	}
}

